﻿using Microsoft.Owin;
using Owin;
using System.Threading.Tasks;

[assembly: OwinStartup(typeof(Sidvall.Web.IO.Host.Startup))]

namespace Sidvall.Web.IO.Host
{
    public partial class Startup
    {
        #region Configuration

        public void Configuration(IAppBuilder app)
        {
            RegisterTypesAsync().Wait();

            ConfigureAuth(app);
            ConfigureWebApi(app);
        }

        #endregion
        #region RegisterTypes

        private static async Task RegisterTypesAsync()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            var log = new Sidvall.Logging.LogCollection();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                //AccountManager = new Sidvall.Security.AccountManager(new IVL.IdentityServer.Security.AccountProvider()),
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                //AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                AuthenticationStrategyManager = new Sidvall.Security.AuthenticationStrategyManager(new Sidvall.Security.AuthenticationStrategy()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                //HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                LogManager = new Sidvall.Logging.LogManager(log),
                //SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                //SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient(Properties.Settings.Default.SmsWebAddress)),
                //SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SqlManager = new Sidvall.Data.SqlManager(new Sidvall.Data.SqlServerProvider()),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                UserResponseStrategyManager = new Sidvall.Security.UserResponseStrategyManager(new Sidvall.Security.UserResponseStrategy()),
                //UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

                // Server
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Web.Security.PrincipalContextProvider()),
                PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Web.Security.PrincipalProvider()),

                // Host specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.Web.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.Web.IO.Host.Configuration.ApplicationSettings()),
            };

            //// File storage Web API
            //Sidvall.Web.IO.SystemContext.Current.FileSystemContext.Stores.Add("1", "", null, null);
            //Sidvall.Web.IO.SystemContext.Current.StorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.AzureFileSystem());
            //Sidvall.IO.AzureSystemContext.Current.StorageConnectionString = @"";

            // Application specific
            var serviceManager = Sidvall.SystemContext.Current.CodeManager.CreateInstance(Properties.Settings.Default.ServiceManager) as IServiceManager;
            if (serviceManager == null)
                serviceManager = new ServiceManager();
            await serviceManager.InitializeAsync(Properties.Settings.Default.ServiceManagerContext);
            Sidvall.Web.IO.SystemContext.Current.FileSystemContext.Stores.AddRange(serviceManager.Stores);
            if (serviceManager.AuthorizationManager != null)
                Sidvall.Web.IO.SystemContext.Current.AuthorizationManager = serviceManager.AuthorizationManager;
        }

        #endregion
    }
}